package com.dwt.hrmanager.web;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.dwt.hrmanager.model.TUser;
import com.dwt.hrmanager.service.IUserService;
import com.dwt.hrmanager.util.Constants;

/*
 * 不需要实现任何接口，也不需要继承任何的类，也不需要任何 Servlet API
 */
@Controller
@RequestMapping("/welcome")
//将Model中属性名为Constants.USER_INFO_SESSION的属性放到Session属性列表中，以便这个属性可以跨请求访问
@SessionAttributes(Constants.USER_INFO_SESSION)
public class WelcomeController {
	
	private IUserService userService;
	
	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	
	@RequestMapping(value = "/login",method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
	public String login(TUser user,Model model) throws Exception {
		TUser user1 = userService.getUserByName(user.getName());
		if(user1 == null) {
			//model.addAttribute("message", "用户不存在");
            return "用户不存在";
		}else if(user.getPassword() == null || !user.getPassword().equals(user1.getPassword()) ){
			//model.addAttribute("message", "密码错误");
            return "密码错误";
		}else {
			model.addAttribute(Constants.USER_INFO_SESSION, user1); //名为Constants.USER_INFO_SESSION的属性放到Session属性列表中
            return "000000";
		}
	}

    @RequestMapping(value = "/loginSuc",method=RequestMethod.GET)
    public String helloWorld() throws Exception {
        return "view/welcome";
    }
}
